home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / CIncludesTool 1.0 / original version / source files / dictionaries.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-29  |  5.6 KB  |  268 lines  |  [TEXT/MPS ]

  1.  
  2. #include "CIncludesCode.h"
  3. #include <Memory.h>
  4. #include <StdIO.h>
  5.  
  6.  
  7. extern long            totalWords;
  8. extern long            numFiles;
  9. extern ptrArray        *argvPtr;
  10. extern strArray     **filesHdl;
  11. extern Handle        dictionary[numDictionaries];
  12. extern Handle        dependencies;
  13.  
  14.  
  15.  
  16. short dictionaryIndex( char ch )
  17. {
  18.     if ( ch == '_' )
  19.         return 0;
  20.     else
  21.         return ( (ch & 0xDF) - '@' );
  22. }
  23.  
  24.  
  25. void insertWord( short fileIndex, long filePosition, char *s )
  26. {
  27.     Handle    dictHdl;
  28.     long    curSize;
  29.     long    endOffset;
  30.     char    *p;
  31.     short    len = strlen( s );
  32.     
  33.     if ( len == 0 )  return;
  34.  
  35.     totalWords++;
  36.     
  37.     dictHdl = dictionary[ dictionaryIndex( *s ) ];
  38.     curSize = GetHandleSize( dictHdl );
  39.     endOffset = *((long*) (*dictHdl));
  40.     
  41.     if ( ( 8 + endOffset + len + 1 ) > curSize )
  42.     {
  43.         SetHandleSize( dictHdl, curSize + 1024 );
  44.         if ( MemError() )
  45.         {
  46.             fprintf( stderr, "MemError = %d\n", MemError() );
  47.             errorExit ( "Could not resize dictionary handle!" );
  48.         }
  49.     }
  50.  
  51.     p = *dictHdl + endOffset;
  52.     putShort( p, fileIndex );
  53.     putLong( (p + 2), filePosition );
  54.     strcpy( (p + (2 + 4) ), s );
  55.     
  56.     *((long*) (*dictHdl)) += ( (2 + 4 + 1) + len);  // fileIndex = 2, filePostion = 4, '\0' = 1
  57. }
  58.  
  59.  
  60. #define W(s)  insertWord( -1, 0, s )
  61.  
  62. void fillReservedWords()
  63. {
  64.     W("asm");            W("double");        W("int");            W("sizeof");
  65.     W("auto");            W("else");            W("long");            W("static");
  66.     W("break");            W("entry");            W("new");            W("struct");
  67.     W("case");            W("enum");            W("operator");        W("switch");
  68.     W("catch");            W("extended");        W("overload");        W("template");
  69.     W("char");            W("extern");        W("pascal");        W("this");
  70.     W("class");            W("float");            W("private");        W("typedef");
  71.     W("comp");            W("for");            W("protected");        W("union");
  72.     W("const");            W("friend");        W("public");        W("unsigned");
  73.     W("continue");        W("goto");            W("register");        W("virtual");
  74.     W("default");        W("if");            W("return");        W("void");
  75.     W("delete");        W("inherited");        W("short");            W("volatile");
  76.     W("do");            W("inline");        W("signed");        W("while");
  77. }
  78.  
  79.  
  80. void initDictionaries()
  81. {
  82.     short i;
  83.     
  84.     fprintf( stderr, "Initializing dictionaries...\n" );    
  85.  
  86.     for ( i = 0; i < numDictionaries; ++i )
  87.     {
  88.         dictionary[i] = NewHandle( 4 );
  89.         *((long*) (*dictionary[i])) = 4;   /* "End Pointer" */
  90.     }
  91.  
  92.     fillReservedWords();
  93. }
  94.  
  95.  
  96. void DisposDictionaries()
  97. {
  98.     short i;
  99.     
  100.     for ( i = 0; i < numDictionaries; ++i )
  101.         DisposHandle( dictionary[i] );
  102. }
  103.  
  104.  
  105. char *extractIdentifier( char *dest, char *src)
  106. {
  107.     char    *destPtr = dest;
  108.     short    count;
  109.     
  110.     for ( count = 0; validChar( *src ) && (count < 64); ++count )
  111.         *dest++ = *src++;
  112.     if ( *src == '.' )       // return NULL string for structure members, etc.
  113.     {
  114.         while ( (*src == '.') || validChar( *src ) )
  115.             src++;
  116.         *destPtr = '\0';
  117.     }
  118.     else
  119.         *dest = '\0';
  120.  
  121.     return src;
  122. }
  123.  
  124.  
  125. long nextIdentifier( char *s, Handle dataHdl, long offset, long totalSize )
  126. { // parse text looking for valid identifiers
  127.  
  128.     char *base  = StripAddress(*dataHdl);
  129.     char *p     = base + offset;
  130.     char *q     = base + totalSize;
  131.     char ch;
  132.  
  133.     while ( p < q )
  134.     {
  135.         ch = *p++;
  136.  
  137.         if ( ch == '"' )                                      // skip string literals
  138.             while ( (*p++ != '"') || (*(p-2) == '\\') ) ;
  139.             
  140.         else if ( ch == '#' )                                 // skip pre-processor commands
  141.             while ( validChar(*p++) ) ;
  142.             
  143.         else if ( (ch == '/') && (*p == '/') )                 // skip comments
  144.             while ( *p++ != '\n' ) ;
  145.         
  146.         else if ( (ch == '/') && (*p == '*') )                 // skip comments
  147.             while ( (*p++ != '/') || (*(p-2) != '*') ) ;
  148.         
  149.         else if ( validStart( ch ) )
  150.         {
  151.             p = extractIdentifier( s, p - 1 );
  152.             if ( *s )
  153.                 return ( (long) ( p - base ) );
  154.         }
  155.     }
  156.     return totalSize;
  157. }
  158.  
  159.  
  160.  
  161. long searchDict( char *s, Handle dictHdl )  // returns offset or 0
  162. {
  163.     char *base  = StripAddress(*dictHdl);
  164.     char *p     = base + (4 + 2 + 4);        // endOffset = 4, fileIndex = 2, filePostion = 4; 
  165.     char *limit    = base + *((long*) base);
  166.  
  167.     if ( *s )
  168.         while ( p < limit )
  169.             if ( strcmp( p, s ) == 0 )
  170.                 return ( (long) (p - base - (2 + 4) ) );    // fileIndex = 2, filePostion = 4
  171.             else
  172.                 p += strlen( p ) + (2 + 4 + 1);  // fileIndex = 2, filePostion = 4, '\0' = 1
  173.     return 0;
  174. }
  175.  
  176.  
  177. long parseFile( Handle dataHdl, short fileIndex )
  178. {
  179.     short    oldFile;
  180.     long     offset;
  181.     long    count = 0;
  182.     long    pos   = 0;
  183.     long    totalSize = GetHandleSize( dataHdl ) - 1;
  184.     Handle    dictHdl;
  185.     char     s[64];
  186.  
  187.     while ( (pos = nextIdentifier( s, dataHdl, pos, totalSize )) < totalSize )
  188.     {
  189.         dictHdl = dictionary[dictionaryIndex( *s )];
  190.  
  191.         if ( offset = searchDict( s, dictHdl ) )
  192.         {
  193.             oldFile = getShort( *dictHdl + offset );
  194.             
  195.             if ( (oldFile != -1) &&
  196.                     (oldFile != fileIndex) &&
  197.                         isDependent( *dependencies, oldFile, fileIndex ) )
  198.             {
  199.                 putShort( (*dictHdl + offset), fileIndex );
  200.                 putLong( (*dictHdl + offset + 2), pos );
  201.             }
  202.         }
  203.         else
  204.         {
  205.             count++;
  206.             insertWord( fileIndex, pos, s );
  207.         }
  208.     }
  209.     return count;
  210. }
  211.  
  212.  
  213. void fillDictionaries()
  214. {
  215.     short    i;
  216.     short    maxLength = maxFilename();
  217.     long    count = 0;
  218.     Handle     dataHdl;
  219.     
  220.     fprintf( stderr, "\nParsing Files:\n" );
  221.  
  222.     for ( i = 0; i < numFiles; ++i )
  223.     {
  224.         fprintf( stderr, "    %-*s", maxLength, (**filesHdl)[i] );
  225.  
  226.         dataHdl = loadDataFile( (*argvPtr)[i + 2] );
  227.         count = parseFile( dataHdl, i );
  228.         DisposHandle( dataHdl );
  229.         
  230.         fprintf( stderr, "  %4d new\n", count);
  231.     }
  232.     
  233.     fprintf( stderr, "\nTotal Entries = %ld\n", totalWords );
  234. }
  235.  
  236.  
  237. void writeDictionary( Handle dictHdl )
  238. {
  239.     long pos = 4;
  240.     long endOffset = *((long*) (*dictHdl));
  241.     char s[64];
  242.     
  243.     while ( pos < endOffset )
  244.     {
  245.         fprintf( stderr, "%3d  ", getShort( *dictHdl + pos ) );
  246.         pos += 2;
  247.         strcpy( s, *dictHdl + pos);
  248.         fprintf( stderr, "%s\n", s );
  249.         pos += strlen( s ) + 1;
  250.     }
  251. }
  252.  
  253.  
  254. void writeAllDictionaries()
  255. {
  256.     short i;
  257.     
  258.     for ( i = 0; i < numDictionaries; ++i )
  259.         writeDictionary( dictionary[i] );
  260. }
  261.  
  262.  
  263. void writeSpecificDirectory( char ch )
  264. {
  265.     writeDictionary( dictionary[dictionaryIndex( ch )] );
  266. }
  267.  
  268.